package com.objcat.servicesecurity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.objcat.servicesecurity.entity.po.*;
import com.objcat.servicesecurity.entity.vo.RoleVo;
import com.objcat.servicesecurity.entity.vo.UserVo;
import com.objcat.servicesecurity.mapper.RoleMapper;
import com.objcat.servicesecurity.service.RolePowerService;
import com.objcat.servicesecurity.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.objcat.servicesecurity.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author misheep
 * @since 2020-07-29
 */
@Slf4j
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RolePo> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RolePowerService rolePowerService;

    /**
     * 获取用户的角色
     * @param userPo
     * @return
     */
    @Override
    public RoleVo getRole(UserPo userPo) {
        UserRolePo userRolePo = userRoleService.getUserRole(userPo);
        //此处发生过错误：因主键名不为id，故mp的selectById获得空值
        QueryWrapper<RolePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",userRolePo.getRoleId());
        RolePo rolePo = roleMapper.selectOne(queryWrapper);
        return toRoleVo(rolePo);
    }

    /**
     * 获取资源的角色
     * @param powerPo
     * @return
     */
    @Override
    public List<RoleVo> getRole(PowerPo powerPo) {
        List<RolePowerPo> rolePowerPoList = rolePowerService.getRolePower(powerPo);
        log.info("根据powerPo获得的关联list大小：{}",rolePowerPoList.size());
        log.info(rolePowerPoList.toString());
        if(rolePowerPoList.isEmpty()) {
            return new ArrayList<RoleVo>();
        }
        QueryWrapper<RolePo> queryWrapper = new QueryWrapper<>();
        rolePowerPoList.forEach(rolePowerPo -> {
            queryWrapper.eq("role_id",rolePowerPo.getRoleId()).or();
//            if(rolePowerPoList.size()-1 != rolePowerPoList.indexOf(rolePowerPo)) {
//                queryWrapper.or();
//            }
        });
        List<RolePo> rolePoList = roleMapper.selectList(queryWrapper);
        return toRoleVoList(rolePoList);
    }

    /**
     * 转换成Vo
     * @param rolePo
     * @return
     */
    @Override
    public RoleVo toRoleVo(RolePo rolePo) {
        if(rolePo==null) {
            return null;
        }
        RoleVo roleVo = new RoleVo();
        roleVo.setRoleId(rolePo.getRoleId());
        roleVo.setRoleName(rolePo.getRoleName());
        return roleVo;
    }

    @Override
    public List<RoleVo> toRoleVoList(List<RolePo> rolePoList) {
        List<RoleVo> roleVoList = new ArrayList<>();
        rolePoList.forEach(rolePo -> {
            roleVoList.add(toRoleVo(rolePo));
        });
        return roleVoList;
    }


}
